package com.cg.design.graphicdesignpattern.interpreter.language;

/**
 * 对应<repeat command>标签的类，继承了BaseNode类
 *
 * @program: LeetCode->RepeatCommandNode
 * @author: cg
 * @create: 2022-04-06 08:53
 **/
public class RepeatCommandNode extends BaseNode {

    /**
     * 保存<number>中的字段
     */
    private int number;

    /**
     * 保存<command list>中的字段
     */
    private BaseNode commandListNode;

    @Override
    public void parse(Context context) throws ParseException {
        context.skipToken("repeat");
        number = context.currentNumber();
        context.nextToken();
        commandListNode = new CommandListNode();
        commandListNode.parse(context);
    }

    @Override
    public void execute() throws ExecuteException {
        for (int i = 0; i < number; i++) {
            commandListNode.execute();
        }
    }

    @Override
    public String toString() {
        return "[repeat " + number + " " + commandListNode + ']';
    }
}
